package com.huluwa.picture.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huluwa.common.core.domain.entity.SysUser;
import com.huluwa.picture.exception.ErrorCode;
import com.huluwa.picture.exception.ThrowUtils;
import com.huluwa.picture.mapper.WebCollectionMapper;
import com.huluwa.picture.model.dto.webcollection.WebCollectionAddReqeuest;
import com.huluwa.picture.model.dto.webcollection.WebCollectionQueryRequest;
import com.huluwa.picture.model.entity.WebCollection;
import com.huluwa.picture.model.vo.UserVO;
import com.huluwa.picture.model.vo.WebCollectionVO;
import com.huluwa.picture.service.WebCollectionService;
import com.huluwa.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author luyifan
* @description 针对表【web_collection(网站收藏)】的数据库操作Service实现
* @createDate 2025-02-01 00:12:33
*/
@Service
public class WebCollectionServiceImpl extends ServiceImpl<WebCollectionMapper, WebCollection>
    implements WebCollectionService {


    @Resource
    private ISysUserService userService;

    @Override
    public Integer addWebCollection(WebCollectionAddReqeuest webCollectionAddReqeuest) {
        ThrowUtils.throwIf(webCollectionAddReqeuest == null,ErrorCode.PARAMS_ERROR);
        WebCollection webCollection = new WebCollection();
        BeanUtils.copyProperties(webCollectionAddReqeuest,webCollection);

        this.validWebCollection(webCollection);
        this.fillWebCollection(webCollection);
        boolean result = this.save(webCollection);
        ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
        return webCollection.getId();
    }

    @Override
    public void validWebCollection(WebCollection webCollection) {
        ThrowUtils.throwIf(webCollection == null, ErrorCode.PARAMS_ERROR);
        String category = webCollection.getCategory();
        String link = webCollection.getLink();
        String name = webCollection.getName();
        ThrowUtils.throwIf(StrUtil.isBlank(category),ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(StrUtil.isBlank(link),ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(StrUtil.isBlank(name),ErrorCode.PARAMS_ERROR);
    }


    @Override
    public QueryWrapper<WebCollection> getSearchQueryWrapper(WebCollectionQueryRequest webCollectionQueryRequest){
        QueryWrapper queryWrapper =  new QueryWrapper();
        String category = webCollectionQueryRequest.getCategory();
        String name = webCollectionQueryRequest.getName();
        String link = webCollectionQueryRequest.getLink();
        String sortField = webCollectionQueryRequest.getSortField();
        String sortOrder = webCollectionQueryRequest.getSortOrder();
        queryWrapper.eq(StrUtil.isNotBlank(category),"category",category);
        queryWrapper.like(StrUtil.isNotBlank(name),"name",name);
        queryWrapper.eq(StrUtil.isNotBlank(link),"link",link);

        // 排序字段
        queryWrapper.orderBy(StrUtil.isNotBlank(sortField), sortOrder.equals("ascend"), sortField);
        return queryWrapper;
    }


    @Override
    public void fillWebCollection(WebCollection webCollection) {
       if (StrUtil.isBlank(webCollection.getIcon())) {
           // TODO 根据网址获取网站图标
       }

       if(webCollection.getVisible() == null){
           webCollection.setVisible(1);
       }
    }


    @Override
    public Page<WebCollectionVO> getWebCollectionPage(Page<WebCollection> page) {
        List<WebCollection> webCollectionList = page.getRecords();
        // 构造包装类pageVO对象
        Page<WebCollectionVO>  webCollectionPage = new Page<>(page.getCurrent(), page.getSize(),page.getTotal());

        if(webCollectionList.isEmpty()) {
            return webCollectionPage;
        }

        List<WebCollectionVO> webCollectionVOList = webCollectionList.stream().map(WebCollectionVO::objToVO).collect(Collectors.toList());
        // 设置用户信息
        Set<Long> userIdSet = webCollectionList.stream().map(WebCollection::getUserId).collect(Collectors.toSet());

        Map<Long, List<SysUser>> sysUserMap = userService.selectUserListByUserIds(userIdSet).stream().collect(Collectors.groupingBy(SysUser::getUserId));

        webCollectionVOList.forEach(webCollectionVO -> {
            Long userId = webCollectionVO.getUserId();
            UserVO userVO = new UserVO();

            if (sysUserMap.containsKey(userId)) {
                SysUser sysUser = sysUserMap.get(userId).get(0);
                BeanUtils.copyProperties(sysUser,userVO);
            }
            webCollectionVO.setUserVO(userVO);
        });
        webCollectionPage.setRecords(webCollectionVOList);
        return webCollectionPage;

    }

    @Override
    public WebCollectionVO getWebCollectionVO(WebCollection webCollection){
        WebCollectionVO webCollectionVO = WebCollectionVO.objToVO(webCollection);
        // 关联查询用户信息
        Long userId = webCollection.getUserId();
        if (userId != null && userId > 0) {
            UserVO userVO = new UserVO();
            SysUser sysUser = userService.selectUserById(userId);
            BeanUtils.copyProperties(sysUser,userVO);
            webCollectionVO.setUserVO(userVO);
        }
        return webCollectionVO;
    }

}




